package com.sandu.module_mall.frament

import android.os.Bundle
import android.view.LayoutInflater
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ConvertUtils
import com.kingja.loadsir.core.LoadService
import com.sandu.lib_common.base.BaseVBFragment
import com.sandu.lib_common.base.appViewModel
import com.sandu.lib_common.base.eventViewModel
import com.sandu.lib_common.bean.BannerResponse
import com.sandu.lib_common.bean.CollectBus
import com.sandu.lib_common.ext.init
import com.sandu.lib_common.ext.initFloatBtn
import com.sandu.lib_common.ext.initFooter
import com.sandu.lib_common.ext.loadListData
import com.sandu.lib_common.ext.loadServiceInit
import com.sandu.lib_common.ext.setAdapterAnimation
import com.sandu.lib_common.ext.setUiTheme
import com.sandu.lib_common.ext.showLoading
import com.sandu.lib_common.ext.showMessage
import com.sandu.lib_common.utils.JumpUtils
import com.sandu.lib_common.webview.BaseWebActivity
import com.sandu.lib_common.webview.WebActivity
import com.sandu.lib_mvvm.ext.parseState
import com.sandu.lib_ui.view.recyclerview.DefineLoadMoreView
import com.sandu.lib_ui.view.recyclerview.SpaceItemDecoration
import com.sandu.module_mall.adapter.AriticleAdapter
import com.sandu.module_mall.databinding.FragmentHomeBinding
import com.sandu.module_mall.viewmodel.HomeViewModel
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import com.zhpan.bannerview.BannerViewPager
import com.sandu.module_mall.R
import com.sandu.module_mall.view.banner.HomeBannerAdapter
import com.sandu.module_mall.view.banner.HomeBannerViewHolder
import com.sandu.module_mall.viewmodel.RequestCollectViewModel
import com.sandu.module_mall.viewmodel.RequestHomeViewModel


/**
 * 作者　: sandu
 * 时间　: 2024/12/27
 * 描述　:
 */
class HomeFragment(mainViewpager: ViewPager2) :
    BaseVBFragment<HomeViewModel, FragmentHomeBinding>() {

    //适配器
    private val articleAdapter: AriticleAdapter by lazy { AriticleAdapter(arrayListOf(), true) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    //recyclerview的底部加载view 因为在首页要动态改变他的颜色，所以加了他这个字段
    private lateinit var footView: DefineLoadMoreView

    //收藏viewModel
    private val requestCollectViewModel: RequestCollectViewModel by viewModels()

    //请求数据ViewModel
    private val requestHomeViewModel: RequestHomeViewModel by viewModels()

    override fun initView(savedInstanceState: Bundle?) {
        //状态页配置
        loadsir = loadServiceInit(mViewBind.includeList.includeRecyclerview.swipeRefresh) {
            //点击重试时触发的操作
            loadsir.showLoading()
            requestHomeViewModel.getBannerData()
            requestHomeViewModel.getHomeData(true)
        }
        //初始化
        mViewBind.includeToolbar.toolbar.run {
            init("首页")
        }
        //初始化recyclerView
        mViewBind.includeList.includeRecyclerview.recyclerView.init(
            LinearLayoutManager(context),
            articleAdapter
        ).let {
            //因为首页要添加轮播图，所以我设置了firstNeedTop字段为false,即第一条数据不需要设置间距
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f), false))
            footView = it.initFooter(SwipeRecyclerView.LoadMoreListener {
                requestHomeViewModel.getHomeData(false)
            })
            //初始化FloatingActionButton
            it.initFloatBtn(mViewBind.includeList.floatbtn)
        }
        //初始化 SwipeRefreshLayout
        mViewBind.includeList.includeRecyclerview.swipeRefresh.init {
            //触发刷新监听时请求数据
            requestHomeViewModel.getHomeData(true)
        }
        articleAdapter.run {
            setCollectClick { item, v, _ ->
                if (v.isChecked) {
                    requestCollectViewModel.uncollect(item.id)
                } else {
                    requestCollectViewModel.collect(item.id)
                }
            }
            setOnItemClickListener { adapter, view, position ->
                // TODO:
                activity?.let { JumpUtils.jumpTo(it, BaseWebActivity::class.java) }
            }
            addChildClickViewIds(R.id.item_home_author, R.id.item_project_author)
            setOnItemChildClickListener { adapter, view, position ->
                when (view.id) {
                    R.id.item_home_author, R.id.item_project_author -> {
                        // TODO:

                    }
                }
            }
        }
    }

    /**
     * 懒加载
     */
    override fun lazyLoadData() {
        //设置界面 加载中
        loadsir.showLoading()
        //请求轮播图数据
        requestHomeViewModel.getBannerData()
        //请求文章列表数据
        requestHomeViewModel.getHomeData(true)
    }

    override fun createObserver() {
        requestHomeViewModel.run {
            //监听首页文章列表请求的数据变化
            homeDataState.observe(viewLifecycleOwner, Observer {
                //设值 新写了个拓展函数，搞死了这个恶心的重复代码
                loadListData(
                    it,
                    articleAdapter,
                    loadsir,
                    mViewBind.includeList.includeRecyclerview.recyclerView,
                    mViewBind.includeList.includeRecyclerview.swipeRefresh
                )
            })
            //监听轮播图请求的数据变化
            //监听轮播图请求的数据变化
            bannerData.observe(viewLifecycleOwner, Observer { resultState ->
                parseState(resultState, { data ->
                    //请求轮播图数据成功，添加轮播图到headview ，如果等于0说明没有添加过头部，添加一个
                    if (mViewBind.includeList.includeRecyclerview.recyclerView.headerCount == 0) {
                        val headview =
                            LayoutInflater.from(context).inflate(R.layout.include_banner, null)
                                .apply {
                                    findViewById<BannerViewPager<BannerResponse, HomeBannerViewHolder>>(
                                        R.id.banner_view
                                    ).apply {
                                        adapter = HomeBannerAdapter()
                                        //设置自定义indicator
//                                        setIndicatorView(null)
                                        setLifecycleRegistry(lifecycle)
                                        setOnPageClickListener {
                                            //todo
                                        }
                                        create(data)
                                    }
                                }
                        mViewBind.includeList.includeRecyclerview.recyclerView.addHeaderView(
                            headview
                        )
                        mViewBind.includeList.includeRecyclerview.recyclerView.scrollToPosition(0)
                    }
                })
            })
        }
        requestCollectViewModel.collectUiState.observe(viewLifecycleOwner, Observer {
            if (it.isSuccess) {
                //收藏或取消收藏操作成功，发送全局收藏消息
                eventViewModel.collectEvent.value = CollectBus(it.id, it.collect)
            } else {
                showMessage(it.errorMsg)
                for (index in articleAdapter.data.indices) {
                    if (articleAdapter.data[index].id == it.id) {
                        articleAdapter.data[index].collect = it.collect
                        articleAdapter.notifyItemChanged(index)
                        break
                    }
                }
            }
        })
        appViewModel.run {
            //监听账户信息是否改变 有值时(登录)将相关的数据设置为已收藏，为空时(退出登录)，将已收藏的数据变为未收藏
            userInfo.observeInFragment(this@HomeFragment, Observer {
                if (it != null) {
                    it.collectIds.forEach { id ->
                        for (item in articleAdapter.data) {
                            if (id.toInt() == item.id) {
                                item.collect = true
                                break
                            }
                        }
                    }
                } else {
                    for (item in articleAdapter.data) {
                        item.collect = false
                    }
                }
                articleAdapter.notifyDataSetChanged()
            })
            //监听全局的主题颜色改变
            appColor.observeInFragment(this@HomeFragment) {
                setUiTheme(
                    it,
                    mViewBind.includeToolbar.toolbar,
                    mViewBind.includeList.floatbtn,
                    mViewBind.includeList.includeRecyclerview.swipeRefresh,
                    loadsir,
                    footView
                )
            }
            //监听全局的列表动画改编
            appAnimation.observeInFragment(this@HomeFragment) {
                articleAdapter.setAdapterAnimation(it)
            }
            //监听全局的收藏信息 收藏的Id跟本列表的数据id匹配则需要更新
            eventViewModel.collectEvent.observeInFragment(this@HomeFragment) {
                for (index in articleAdapter.data.indices) {
                    if (articleAdapter.data[index].id == it.id) {
                        articleAdapter.data[index].collect = it.collect
                        articleAdapter.notifyItemChanged(index)
                        break
                    }
                }
            }
        }
    }
}